<script setup>
import { ref, onMounted, watch, computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
const router = useRouter()
const route = useRoute()
const user = ref(JSON.parse(sessionStorage.getItem('user') || 'null'))
function logout() {
  sessionStorage.removeItem('user')
  user.value = null
  router.push('/login')
}
const refreshUser = () => {
  user.value = JSON.parse(sessionStorage.getItem('user') || 'null')
}
onMounted(() => {
  window.addEventListener('storage', refreshUser)
  refreshUser()
})
watch(route, refreshUser)
const isLoginPage = computed(() => route.path === '/login')
</script>

<template>
  <div v-if="isLoginPage">
    <router-view />
  </div>
  <div v-else>
    <header class="header">
      <span>旅游景点售票管理系统</span>
      <span v-if="user && user.username">你好，{{ user.username }} <button class="logout-btn" @click="logout">退出</button></span>
    </header>
    <div class="container">
      <aside class="sidebar">
        <nav>
          <ul>
            <li v-if="user && user.role === 'ADMIN'"><router-link to="/users">用户管理</router-link></li>
            <li v-if="user && user.role === 'ADMIN'"><router-link to="/scenic-spots">景点管理</router-link></li>
            <li v-if="user && user.role === 'ADMIN'"><router-link to="/buy">门票购买</router-link></li>
            <li v-if="user && user.role === 'USER'"><router-link to="/buy">购买门票</router-link></li>
            <li v-if="user"><router-link to="/orders">订单管理</router-link></li>
          </ul>
        </nav>
      </aside>
      <main class="main">
        <router-view />
      </main>
    </div>
  </div>
</template>

<style>
.header {
  height: 56px;
  background: #409eff;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 32px;
}
.container {
  display: flex;
  height: calc(100vh - 56px);
  width: 100vw;
}
.sidebar {
  width: 200px;
  background: #232f3e;
  color: #fff;
  padding: 32px 0 0 0;
  height: 100%;
  box-shadow: 2px 0 8px rgba(0,0,0,0.04);
}
.sidebar ul {
  list-style: none;
  padding: 0 0 0 0;
  margin: 0;
}
.sidebar li {
  margin: 0 0 8px 0;
  padding: 0;
}
.sidebar a {
  display: block;
  color: #fff;
  text-decoration: none;
  padding: 14px 32px 14px 32px;
  border-radius: 0 24px 24px 0;
  font-size: 16px;
  font-weight: 500;
  transition: background 0.2s, color 0.2s;
  position: relative;
}
.sidebar a.router-link-active,
.sidebar a:hover {
  background: #409eff;
  color: #fff;
  font-weight: bold;
}
.sidebar li:not(:last-child) {
  border-bottom: 1px solid #2d3a4b;
}
.main {
  flex: 1;
  padding: 0;
  background: #f5f6fa;
  overflow: auto;
  min-width: 0;
  height: 100%;
  display: block;
}
.el-card {
  width: 100%;
  max-width: none;
  margin: 0;
  box-shadow: none;
  border-radius: 0;
}
.logout-btn {
  margin-left: 16px;
  background: #fff;
  color: #409eff;
  border: none;
  border-radius: 4px;
  padding: 4px 12px;
  cursor: pointer;
  font-size: 14px;
  transition: background 0.2s;
}
.logout-btn:hover {
  background: #e6f0fa;
}
</style>
